package com.galgram.project.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import com.galgram.project.entity.CourseDetail;
import com.galgram.project.service.IFxCourseDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.FxCourse;
import com.galgram.project.service.IFxCourseService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 课程Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/course")
public class FxCourseController extends BaseController {
    @Autowired
    private IFxCourseService fxCourseService;
    @Autowired
    private IFxCourseDetailService courseDetailService;

    /**
     * 查询课程列表
     */
    @GetMapping("/getList")
    public TableDataInfo getCourseList(FxCourse fxCourse) {
        startPage();
        List<FxCourse> list = fxCourseService.getCourseList(fxCourse);
        return getDataTable(list);
    }


    /**
     * 查询课程详情
     */
    @GetMapping("/getCourseDetail/{id}")
    public TableDataInfo getDetail(@PathVariable("id") Long id) {
        CourseDetail courseDetail = courseDetailService.selectDetailByCourseId(id);
        List<CourseDetail> list = new ArrayList<>();
        list.add(courseDetail);
        return getDataTable(list);
    }

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('project:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxCourse fxCourse) {
        startPage();
        List<FxCourse> list = fxCourseService.selectFxCourseList(fxCourse);
        return getDataTable(list);
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxCourseService.getById(id));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('project:course:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxCourse fxCourse) {
        fxCourseService.save(fxCourse);
        return success("添加成功");
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('project:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxCourse fxCourse) {
        fxCourseService.updateById(fxCourse);
        return success("修改成功");
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('project:course:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxCourseService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('project:course:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxCourse fxCourse) {
        List<FxCourse> list = fxCourseService.selectFxCourseList(fxCourse);
        ExcelUtil<FxCourse> util = new ExcelUtil<FxCourse>(FxCourse.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 导入课程列表
     */
    @PreAuthorize("@ss.hasPermi('project:course:import')")
    @Log(title = "课程", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxCourse> util = new ExcelUtil<FxCourse>(FxCourse.class);
        List<FxCourse> userList = util.importExcel(file.getInputStream());
        fxCourseService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:course:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxCourse> list = new ArrayList<FxCourse>();
        ExcelUtil<FxCourse> util = new ExcelUtil<FxCourse>(FxCourse.class);
        util.exportExcel(response, list, "课程数据");
    }
}
